<template>
  <div>
    <button @click="bindBaiduNetDisk">绑定百度网盘</button>
  </div>
  <el-dialog class="baidu-login" :show-close="false" v-model="bindBaiduDialog" width="700px">
    <template #header></template>
    <iframe :src="baiduLoginUrl" style="border: none; width:700px; height:400px"></iframe>
  </el-dialog>
</template>

<script lang="ts">
import {Options, Vue} from 'vue-class-component';
import {getBaiduInfoByUser} from "@/apis/baidu.api";

@Options({})
export default class BackupRestoreView extends Vue {
  bindBaiduDialog = false;
  baiduLoginUrl = ""

  beforeUnmount() {
    window.removeEventListener('message', this.baiduLoginCallback)
  }

  baiduLoginCallback(event: MessageEvent): void {
    if(event.data.eventType === 'BAIDU_LOGIN_CALLBACK'){
      console.log(event.data.code)
    }
  }

  bindBaiduNetDisk(): void {
    window.addEventListener('message', this.baiduLoginCallback)
    getBaiduInfoByUser().then(value => {
      this.baiduLoginUrl = value;
      this.bindBaiduDialog = true;
    })
  }
}
</script>

<style lang="scss">
.baidu-login {
  .el-dialog__header {
    display: none;
  }

  .el-dialog__body {
    padding: 0;
  }
}
</style>